package com.gongjian.web.dcontroller;

import com.gongjian.database.dservice.DcourseService;
import com.gongjian.database.entity.Activity;
import com.gongjian.database.entity.Course;
import com.gongjian.database.entity.Register;
import com.gongjian.database.entity.User;
import com.gongjian.utile.CodeEntity;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author donting
 * @Description
 * @create 2019-03-31 11:49
 */
@RestController
public class DcourseController {

    @Autowired
    DcourseService dcourseService;
 @Autowired
 SqlSession sqlSession;

    //定义上课时间
   String[]  classTime={"08:30:00","10:30:00","14:10:00","16:00:00","18:30:00"};



    @RequestMapping("/course/AllCourse")
    public String getCourse(HttpSession session){

        User user=(User)session.getAttribute("user");
        //获取登录的用户
            return dcourseService.getCours(user.getUserId());
    }
    @RequestMapping("/course/updata")
    public String updata(Course course, HttpSession session){

        User user=(User)session.getAttribute("user");
        sqlSession.update("updata",course);
        sqlSession.commit();

        return new CodeEntity(200,"ok").toJSONString();
    }
@RequestMapping("/course/delete")
    public String delete(Course course, HttpSession session){

        User user=(User)session.getAttribute("user");
        sqlSession.update("delete",course);
        sqlSession.commit();

        return new CodeEntity(200,"ok").toJSONString();
    }
    @RequestMapping("/course/insert")
    public String insert(Course course, HttpSession session){
        User user=(User)session.getAttribute("user");
        course.setUserId(user.getUserId());
        sqlSession.update("insert",course);
        sqlSession.commit();
        return new CodeEntity(200,"ok").toJSONString();
    }
    @RequestMapping("/course/beginRegist")
    public String beginRegist(int courseId,String zhou, HttpSession session) throws ParseException {
        User user=(User)session.getAttribute("user");
        Course course=sqlSession.selectOne("findAllCourseByCourseId",courseId);
        //当前时间
        Date nowDte=new Date();
        String time=classTime[Integer.parseInt(course.getCourseTime().split(",")[1])-1];
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
       //签到时间
        String timee= df.format(nowDte)+" "+time;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

       Date qiandaoTiem=sdf.parse(timee);

        Activity activity=new Activity();
        activity.setActivityName(zhou+" 第"+Integer.parseInt(course.getCourseTime().split(",")[1])+"节   "+course.getCourseNumber());
        activity.setUserId(user.getUserId());
        activity.setActivityRegisterTime(new Timestamp(qiandaoTiem.getTime()));
        activity.setActivityCreateTime(new Timestamp(nowDte.getTime()));
        activity.setClasseId(course.getClasseId());

        Activity res=sqlSession.selectOne("findActivityByUserIdAndActivityName",activity);
        if(res!=null){
            CodeEntity codeEntity=  new CodeEntity(200,"");
            codeEntity.put("activityId",res.getActivityId());
                return codeEntity.toJSONString();
        }


       int a= sqlSession.insert("addActivity",activity);
        sqlSession.commit();
        if(a>0){
            List<Integer> userList=sqlSession.selectList("findUserIdByClasseId",course.getClasseId());
            res=sqlSession.selectOne("findActivityByUserIdAndActivityName",activity);
            if(res!=null){
                for (Integer userId:userList
                     ) {
                    Register register=new Register();
                    register.setActivityId(res.getActivityId());
                    register.setUserId(userId);
                    register.setRegisterState("旷课");
                    sqlSession.insert("addRegister",register);
                }
                sqlSession.commit();
                CodeEntity codeEntity=  new CodeEntity(200,"");
                codeEntity.put("activityId",res.getActivityId());
                return codeEntity.toJSONString();

            }
            //开始创建签到表
        }
        return new CodeEntity(10,"失败").toJSONString();
    }



}
